package com.kxn.multi.module.api.component.rocketmq.consumer;

import com.kxn.multi.module.common.constant.MqConstant;
import com.kxn.multi.module.service.dto.api.mq.MQContentParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 顺序消息
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = MqConstant.TRANSACTION_TOPIC, consumerGroup = MqConstant.TRANSACTION_TOPIC_CONSUMER_GROUP,
        consumeMode = ConsumeMode.ORDERLY, consumeThreadNumber = 1)
public class MqTransactionConsumer implements RocketMQListener<MQContentParam>, RocketMQPushConsumerLifecycleListener {


    @Value("${rocketmq.max-reconsume-times:3}")
    private Integer maxReconsumeTimes;


    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
        // 设置消费最大重试次数
        consumer.setMaxReconsumeTimes(maxReconsumeTimes);
    }

    @Override
    public void onMessage(MQContentParam mqContentParam) {
        String message = mqContentParam.getMessage();
        log.info("topic:{}, 获取到的消息：{}", MqConstant.TRANSACTION_TOPIC, message);
        System.out.println("获取到消息:" + message);
    }

}
